﻿using System;
using System.Reflection;
using JUtil.Extensions;

namespace Test
{


    class ConnectionStringBuilder
    {
        public ConnectionStringBuilder(string connectionString)
        {
            string[] tokens = connectionString.SplitString(";");

            Type type = GetType();
            FieldInfo[] fis = type.GetFields();

            foreach (string token in tokens)
            {
                foreach (FieldInfo fi in fis)
                {
                    string fiName = fi.Name;
                    string fiValue = string.Empty;

                    int pos = token.IndexOf(fiName);

                    if (pos != -1)
                    {
                        pos = pos + fiName.Length + 1;

                        fiValue = token.Substring(pos);

                        fi.SetValue(this, fiValue);

                        break;
                    }
                }
            }
        }
    }

    class MySqlConnectionStringBuilder : ConnectionStringBuilder
    {
        // Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

        public MySqlConnectionStringBuilder(string connectionString)
            : base(connectionString)
        {
        }

        public readonly string Server;
        public readonly string Database;
        public readonly string Uid;
        public readonly string Pwd;
    }

    

    class Program
    {
        static void Main(string[] args)
        {
            MySqlConnectionStringBuilder s = new MySqlConnectionStringBuilder("Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;");

            Type type = s.GetType();
            FieldInfo[] fis = type.GetFields();
            foreach(FieldInfo fi in fis)
            {
                Console.WriteLine(fi.GetValue(s).ToString());
            }

        }
    }
}
